<%@ Language="C#" %>

<%@ Import Namespace="System.IO" %>

<script runat="server">
	void Page_Load( object sender, EventArgs e )
	{
		header.Title = "List Directory (C#)";
		
		if ( Request["dir"] != null )
		{
			DirName.Value = Request["dir"];
		}
	}
</script>
<h3>ASP.NET Directory Browser</h3>
<form runat="server" action="default.aspx">
	Directory Name: <input type="text" id="DirName" size="60" 
				value="c:\program files\internet explorer" runat="server" >
	<input type="submit" value="List">
</form>


<%

	DirectoryInfo dir;
	string anchor;

	// Get the information about the selected directory
	
	dir = new DirectoryInfo( DirName.Value );

	Response.Write("<h3>Sub Directories in " + DirName.Value + "</h3>");

	Response.Write("<table>");
	Response.Write("<tr bgcolor=cornflowerblue>");
 	Response.Write("<td>");
	Response.Write( "Name" );
 	Response.Write("<td>");
	Response.Write( "Last Modified" );
	Response.Write("</tr>");

	
	foreach( DirectoryInfo SubDir in dir.GetDirectories() )
	{
		anchor = "<a href='" + "default.aspx?dir=" + SubDir.FullName + "'>" + SubDir.Name + "</a>";
		Response.Write("<tr>");
 		Response.Write("<td>");
		Response.Write( anchor );
 		Response.Write("<td>");
		Response.Write( SubDir.LastWriteTime );
		Response.Write("</tr>");
	}

	Response.Write("</table>");

	if ( dir.Parent != null )
	{
		anchor = "<a href='" + "default.aspx?dir=" + dir.Parent.FullName + "'>" + dir.Parent.FullName + "</a>";
		Response.Write("<p>Parent directory is " + anchor );
	}

	Response.Write("<h3>Files in " + DirName.Value + "</h3>");

	Response.Write("<table>");
	Response.Write("<tr bgcolor=cornflowerblue>");
 	Response.Write("<td>");
	Response.Write( "Filename" );
 	Response.Write("<td>");
	Response.Write( "Last Modified" );
 	Response.Write("<td>");
	Response.Write( "Size" );
	Response.Write("</tr>");	
	
	foreach( FileInfo f in dir.GetFiles() )
	{
		Response.Write("<tr>");
 		Response.Write("<td>");
		Response.Write( f.Name );
		
		if ( (f.Attributes & FileAttributes.ReadOnly) != 0 )
		{
			Response.Write(" (read only)");
		}
 		Response.Write("<td>");
		Response.Write( f.LastWriteTime );
 		Response.Write("<td>");
		Response.Write( f.Length.ToString() );
		
		Response.Write("</tr>");
	}

	Response.Write("</table>");


%>
